﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Net;
using System.IO;
using System.Threading;
using org.googlecoders.GetLatestChromium.Properties;

namespace org.googlecoders.GetLatestChromium 
{
	public partial class Main: Form
	{
		public Main()
		{
			InitializeComponent();
		}

		private void Main_Load(object sender, EventArgs e)
		{
			Settings settings = new Settings();
			if(settings.CheckOnStart)
				settings.NextCheck = DateTime.Now;
			UpdateLabels();
			UpdateWorker.RunWorkerAsync();
		}

		private void Main_FormClosing(object sender, FormClosingEventArgs e)
		{
			if(e.CloseReason == CloseReason.TaskManagerClosing || e.CloseReason == CloseReason.WindowsShutDown)
				return;
			else
			{
				e.Cancel = true;
				this.Hide();
			}	
		}
		
		private void UpdateButton_Click(object sender, EventArgs e)
		{
			Settings settings = new Settings();
			settings.NextCheck = DateTime.Now;
			settings.Save();
		}
				
		private void UpdateWorker_DoWork(object sender, DoWorkEventArgs e)
		{
			ChromiumUpdate.UpdateThread((BackgroundWorker)sender);
		}

		private void UpdateWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
		{
			if(e.ProgressPercentage == 99)
				ChangeLogText.Text = (string)e.UserState;
			else if(e.ProgressPercentage > 0)
			{
				TrayIcon.ShowBalloonTip(3000, "Chrome Updater Status", StatusLabel.Text, (ToolTipIcon)e.ProgressPercentage);
				StatusLabel.Text = (string)e.UserState;
			}
			else
				StatusLabel.Text = (string)e.UserState;
				
			UpdateLabels();
		}
		
		private void UpdateLabels()
		{
			Settings settings = new Settings();
			InstalledBuildLabel.Text = settings.OurBuild.ToString();
			LatestBuildLabel.Text = settings.TheirBuild.ToString();
			LastCheckedLabel.Text = settings.LastCheck.ToShortDateString() + " " + settings.LastCheck.ToShortTimeString();
			InstalledVersionLabel.Text = ChromiumUpdate.ChromiumVersion;
			TrayIcon.Text = "Last check: " + settings.LastCheck.ToShortDateString() + " " + settings.LastCheck.ToShortTimeString() + System.Environment.NewLine + 
							"Next check: " + settings.NextCheck.ToShortDateString() + " " + settings.NextCheck.ToShortTimeString();
		}

		private void TrayMenuOpen_Click(object sender, EventArgs e)
		{
			this.Show();
		}

		private void TrayMenuSettings_Click(object sender, EventArgs e)
		{
			new SettingsForm().ShowDialog();
		}
		
		private void TrayMenuExit_Click(object sender, EventArgs e)
		{
			this.FormClosing -= new FormClosingEventHandler(Main_FormClosing);
			this.UpdateWorker.CancelAsync();
			this.Close();
		}

		public new void Show()
		{
			this.WindowState = FormWindowState.Normal;
			base.Show();
		}

		public new void Hide()
		{
			base.Hide();
			this.WindowState = FormWindowState.Minimized;
		}

		private void TrayIcon_DoubleClick(object sender, EventArgs e)
		{
			if (this.Visible)
				Hide();
			else
				Show();
		}

		private void SettingsButton_Click(object sender, EventArgs e)
		{
			new SettingsForm().ShowDialog();
		}
	}
}
